package com.mbti.youmore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mbti.youmore.entity.*;
import com.mbti.youmore.mapper.*;
import com.mbti.youmore.service.SearchService;
import com.mbti.youmore.util.AnsjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Guqier
 * @version 1.0
 * @date 2024/5/8 下午9:39
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    PostMapper postMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    LikeMapper likeMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    FollowMapper followMapper;

    @Autowired
    FavoriteMapper favoriteMapper;

    @Override
    public Map<String, Object> globalSearch(String query, Integer userId) {
        Map<String, Object> datas = new LinkedHashMap<>();
        List<Post> posts = postMapper.selectList(null);
        List<User> users = userMapper.selectList(null);
        List<Map<String, Object>> finalPosts = new ArrayList<>();
        List<String> querys = AnsjUtil.startParticiple(query);
        posts.forEach(post -> {
            for (String q : querys) {
                if (post.getContent().contains(q)) {
                    User user = userMapper.selectById(post.getUserId());
                    Map<String, Object> data = new LinkedHashMap<>();
                    data.put("content", post.getContent());
                    data.put("img", post.getImg());
                    data.put("profilePic", user.getProfilePic());
                    data.put("userId", post.getUserId());
                    data.put("name", user.getNickname());
                    data.put("postId", post.getId());
                    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    data.put("date", dateformat.format(post.getPostTime()));
                    data.put("likeNum", post.getLikeNum());
                    data.put("isLike", likeMapper.selectCount(new LambdaQueryWrapper<Like>().eq(Like::getPostId, post.getId()).eq(Like::getUserId, userId)) != 0);
                    data.put("commentNum", commentMapper.selectCount(new LambdaQueryWrapper<Comment>().eq(Comment::getPostId, post.getId())));
                    Favorite favorite = new Favorite();
                    favorite.setPostId(post.getId());
                    favorite.setUserId(userId);
                    data.put("isStaring", favoriteMapper.selectCount(new LambdaQueryWrapper<Favorite>().eq(Favorite::getPostId, favorite.getPostId()).eq(Favorite::getUserId, favorite.getUserId())) != 0);
                    finalPosts.add(data);
                    break;
                }
            }
        });
        List<Map<String, Object>> finalUsers = new ArrayList<>();
        users.forEach(user -> {
            for (String q : querys) {
                if (user.getUsername().contains(q) || user.getNickname().contains(q)) {
                    Map<String, Object> data = new LinkedHashMap<>();
                    data.put("coverPic", user.getCoverPic());
                    data.put("email", user.getEmail());
                    data.put("facebook", user.getFacebook());
                    data.put("nickname", user.getNickname());
                    data.put("password", user.getPassword());
                    data.put("profilePic", user.getProfilePic());
                    data.put("qq", user.getQq());
                    data.put("twitter", user.getTwitter());
                    data.put("userId", user.getUserId());
                    data.put("userLang", user.getUserLang());
                    data.put("userLocation", user.getUserLocation());
                    data.put("username", user.getUsername());
                    data.put("weibo", user.getWeibo());
                    data.put("isFollowing", followMapper.selectCount(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, userId).eq(Follow::getFollowedUserId, user.getUserId())) != 0);
                    finalUsers.add(data);
                }
            }
        });
        Collections.reverse(finalPosts);
        datas.put("posts", finalPosts);
        datas.put("users", finalUsers);
        return datas;
    }

}
